<?php
	ini_set( 'display_errors', 1 ); 
     error_reporting( E_ALL ); 
    include "DB.php";
    include "simple_html_dom.php";
    
 //  $kitco = new simple_html_dom();
 //   $exim = new simple_html_dom();
	
        $ch1 = curl_init('http://www.eximbank.com.vn/WebsiteExrate2012/Exchangerate_vn_2012.aspx');    
      curl_setopt  ($ch1, CURLOPT_HEADER, false); 
      curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);    
      $src1 = curl_exec($ch1);    
      $exim = str_get_html($src1, false);

        $ch2 = curl_init('http://www.kitcosilver.com/');    
      curl_setopt  ($ch2, CURLOPT_HEADER, false); 
      curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);    
      $src2 = curl_exec($ch2);    
      $kitco = str_get_html($src2, false);	

/*  $exim->load_file('http://www.eximbank.com.vn/WebsiteExrate2012/Exchangerate_vn_2012.aspx');
    $kitco->load_file('http://www.kitcosilver.com/');*/

        while (!is_object($exim))
    {
        $ch1 = curl_init('http://www.eximbank.com.vn/WebsiteExrate2012/Exchangerate_vn_2012.aspx');    
      curl_setopt  ($ch1, CURLOPT_HEADER, false); 
      curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);    
      $src1 = curl_exec($ch1);    
      $exim = str_get_html($src1, false);
    }
    $rate_object = $exim->getElementById('ExchangeRateRepeater_ctl01_lblCSHSELLRT');


        while (!is_object($kitco))
    {
                $ch2 = curl_init('http://www.kitcosilver.com/');    
      curl_setopt  ($ch2, CURLOPT_HEADER, false); 
      curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);    
      $src2 = curl_exec($ch2);    
      $kitco = str_get_html($src2, false);	
    }
        $price_object = $kitco->find("font");
        mysql_query("DELETE FROM price WHERE DATE(NOW()) = DATE(time) AND HOUR(time) = HOUR(NOW())");

    $price_string = (string)$price_object[0];
    $price_string = substr($price_string,22,-10).substr($price_string,25,-7);
    $price = floatval($price_string)*0.01;


    $rate_string = (string)$rate_object;
    $rate_string = substr($rate_string,51,-11).substr($rate_string,54,-7);
    $exrate = floatval($rate_string);


    $strQuery = "SELECT * FROM varformula" ;
    $result = mysql_query($strQuery);
    $row = mysql_fetch_row($result);
    $transfer = $row[1];
    $insurance = $row[2];
    $tax = $row[3];
    $rate = $row[4];
    $machining = $row[5];
    $profit = $row[6];
    $sellplus = $row[7];
    $buy = ($price + $price * $transfer + $price * $insurance) * (1 + $tax) * $rate * $exrate + $machining + $profit;
    $buy_price = intval(round($buy/1000)) * 1000;
    $sell = $buy * (1 + $sellplus);
    $sell_price = intval(round($sell/1000)) * 1000;
    
    //sleep(1);
    $result1 = mysql_query("SELECT * FROM price WHERE DATE(time) = DATE(NOW()) AND HOUR(time) = HOUR(NOW())");
    if (mysql_fetch_row($result1))
    {
        echo "Ok";
    }
    else
    {
        mysql_query("INSERT INTO price(international,domestic,sellprice,time) VALUES ('$price','$buy_price','$sell_price',NOW())");
        echo $sell_price;
    }
?>
